<?php
namespace app\examScore\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;
//--教师历年成绩分析
class ExamChartTeacher extends Base {
	
	public function index() {
		$fields = "学期名称,考试名称,班级ID,学科,任课教师,人平分,特优率,优秀率,合格率,低分率,实力,实力排名";
		$list = explode(",", $fields);
		$this->assign('list',$list);
		return $this->fetch();
	}

	public function scoreChart() {
		$teacherName = input("teacherName");
		$this->assign("teacherName", $teacherName);
		return $this->fetch();
	}

	public function pageQuery() {
		$teacherName = input("teacherName");
		if(!$teacherName) return [];
		$page = input("page");
		$rows = input("rows");
		$where["任课教师"] = $teacherName;
		$result = Db::table("成绩汇总")->alias("a")
				  ->join("考试名称 b","a.考试ID=b.ID","INNER")
				  ->join("学期名称 c","b.学期ID=c.ID","INNER")
				  ->join("年级名称 d","b.年级ID=d.ID","INNER")
				  ->field("a.考试ID,学期名称,年级名称+考试名称 考试名称,班级ID,学科,任课教师,Cast(round(人平分,2) as nvarchar(20)) 人平分,Cast(round(特优率,3) as nvarchar(20)) 特优率,Cast(round(优秀率,3) as nvarchar(20)) 优秀率,Cast(round(合格率,3) as nvarchar(20)) 合格率,Cast(round(低分率,3) as nvarchar(20)) 低分率,Cast(convert(decimal(18,2),实力) as nvarchar(20)) 实力,实力排名")
				  ->where($where)
				  ->order("考试ID")
				  ->page($page, $rows)
				  ->select();
		$count = Db::table("成绩汇总")->alias("a")
				  ->join("考试名称 b","a.考试ID=b.ID","INNER")
				  ->join("学期名称 c","b.学期ID=c.ID","INNER")
				  ->where($where)
				  ->count();
		$grid["rows"] = selectResult($result);
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$teacherName = input("teacherName");
		if(!$teacherName) return [];
		$page = input("page");
		$rows = input("rows");
		$where["任课教师"] = $teacherName;
		$result = Db::table("成绩汇总")->alias("a")
				  ->join("考试名称 b","a.考试ID=b.ID","INNER")
				  ->join("学期名称 c","b.学期ID=c.ID","INNER")
				  ->join("年级名称 d","b.年级ID=d.ID","INNER")
				  ->field("a.考试ID,学期名称,年级名称+考试名称 考试名称,班级ID,学科,任课教师,Cast(round(人平分,2) as nvarchar(20)) 人平分,Cast(round(特优率,3) as nvarchar(20)) 特优率,Cast(round(优秀率,3) as nvarchar(20)) 优秀率,Cast(round(合格率,3) as nvarchar(20)) 合格率,Cast(round(低分率,3) as nvarchar(20)) 低分率,Cast(convert(decimal(18,2),实力) as nvarchar(20)) 实力,实力排名")
				  ->where($where)
				  ->order("考试ID")
				  ->select();
		$result = selectResult($result);
		//excel表头内容
		$fields = "学期名称,考试名称,班级ID,学科,任课教师,人平分,特优率,优秀率,合格率,低分率,实力,实力排名";
		$list = explode(",", $fields);
		$header = [];
		foreach ($list as $key => $value){
			$header[$value] = $value;
		}
		Excel::toExcel($result,'教师历年成绩',$header);
		exit;
	}
}
